package EX1;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class demo8 {
    public static void main(String[] args) throws SQLException {
        MysqlDataSource ds=new MysqlDataSource();
        ds.setUrl("jdbc:mysql://localhost:3306/yonghu?useSSL=false");
        ds.setUser("root");
        ds.setPassword("123456");
        Connection conn=ds.getConnection();
        String selectsql="select * from student where id=?";
        PreparedStatement prestmt= conn.prepareStatement(selectsql);

        //1、直接设置
        //prestmt.setInt(1,5);

        //2、用户输入
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入您要查询的id号！");
        int id=scanner.nextInt();
        prestmt.setInt(1,id);

        ResultSet rs= prestmt.executeQuery();
        while (rs.next()){
            int a=rs.getInt(1);
            String b=rs.getString(2);
            System.out.println(a+" "+b);

        }
    }
}
